package com.education.module.teacherExamSetting;

import com.education.domain.*;
import com.education.framework.base.BaseController;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.module.classes.ClassServices;
import com.education.module.examdate.ExamdateServices;
import com.education.module.grade.GradeServices;
import com.education.module.professional.ProfessionalServices;
import com.education.module.teacherType.TeacherTypeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("teacherExamSetting")
public class TeacherExamSettingController extends BaseController{

	@Autowired
	private TeacherExamSettingServices services;

	@Autowired
	private ExamdateServices examdateServices;

	@Autowired
	private ClassServices classServices;

	@Autowired
	private GradeServices gradeServices;

	@Autowired
	private ProfessionalServices professionalServices;

	@Autowired
	private TeacherTypeServices teacherTypeServices;
	
	@RequestMapping(value = "")
	public String list(Model model, SearchParams searchParams,Page page,ServletRequest request){
		List<TeacherExamSetting> list = services.find(searchParams,page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("searchParams", searchParams);
		return "/teacherExamSetting/teacherExamSettingList";
	}
	
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		List<Examdate> examdates = services.findExamdates();
		model.addAttribute("examDateList",examdateServices.find());
		model.addAttribute("action","create");
		model.addAttribute("ids","0");
		return "/teacherExamSetting/teacherExamSettingEdit";
	}

	@RequestMapping(value = "create/{ids}", method = RequestMethod.POST)
	public String create(@PathVariable("ids") String ids, Model model,RedirectAttributes redirectAttributes, HttpServletRequest request) {
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String examDateId = request.getParameter("examDateId");
		TeacherExamSetting setting = null;
		String[] idArrs ;
		if (ids != null && !"".equals(ids)){
			idArrs = ids.split(",");
			for (String teacherId : idArrs) {
				setting = new TeacherExamSetting();
				setting.setCode(Integer.parseInt(code));
				setting.setName(name);
				setting.setContent(content);
				setting.setExamDateId(Integer.parseInt(examDateId));
				setting.setTeacherId(teacherId);
				services.save(setting);
			}
		}
		redirectAttributes.addFlashAttribute("MESSAGE", "选择成功！");
		return "redirect:/teacherExamSetting/update/"+code;
	}

	@RequestMapping(value = "selectTeacher/{ids}/{operateFlag}")
	public String selectTeacher(@PathVariable("ids") String ids,@PathVariable("operateFlag") String operateFlag,SearchParams searchParams,Page page, Model model, HttpServletRequest request) {
		List<Teacher> list = services.findTeachersForSelect(ids, searchParams, page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("operateFlag", operateFlag);
		model.addAttribute("ids", ids);
		model.addAttribute("searchParams", searchParams);
		return "/teacherExamSetting/selectTeacherList";
	}

	@RequestMapping(value = "update/{code}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("code") Integer code,Page page, Model model) {
		List<TeacherExamSetting> teacherExamSettings = services.findByCode(code);
		if (teacherExamSettings != null && teacherExamSettings.size() > 0){
			String ids = "";
			for (TeacherExamSetting teacherExamSetting : teacherExamSettings) {
				if ("".equals(ids)){
					ids += teacherExamSetting.getTeacherId();
				}else{
					ids += ","+teacherExamSetting.getTeacherId();
				}
			}
			List<Teacher> list = services.findTeachers(ids,null,page);
			model.addAttribute("list", list);
			model.addAttribute("page", page);
			model.addAttribute("teacherExamSettings", teacherExamSettings);
			model.addAttribute("teacherExamSetting", teacherExamSettings.get(0));
			model.addAttribute("ids", ids);
		}
		List<Examdate> examdates = services.findExamdates();
		model.addAttribute("examDateList",examdateServices.find());
		model.addAttribute("action", "update");
		int hasResult = services.checkHasScore(code);
		model.addAttribute("hasResult", hasResult);
		return "/teacherExamSetting/teacherExamSettingEdit";
	}

	@RequestMapping(value = "update/{ids}", method = RequestMethod.POST)
	public String update(@PathVariable("ids") String ids, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String examDateId = request.getParameter("examDateId");
		TeacherExamSetting setting = new TeacherExamSetting();
		setting.setCode(Integer.parseInt(code));
		setting.setName(name);
		setting.setContent(content);
		setting.setExamDateId(Integer.parseInt(examDateId));
		services.update(setting);
		services.updateAllSystemData(setting);
		String[] idArrs ;
		if (ids != null && !"".equals(ids)){
			idArrs = ids.split(",");
			for (String teacherId : idArrs) {
				setting = new TeacherExamSetting();
				setting.setCode(Integer.parseInt(code));
				setting.setName(name);
				setting.setContent(content);
				setting.setExamDateId(Integer.parseInt(examDateId));
				setting.setTeacherId(teacherId);
				services.save(setting);
			}
		}
		redirectAttributes.addFlashAttribute("MESSAGE", "选择成功！");
		return "redirect:/teacherExamSetting/update/"+code;
	}

	@RequestMapping(value = "saveFirstStep/{code}")
	public String saveFirstStep(Model model, @PathVariable("code") Integer code,HttpServletRequest request) {
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String examDateId = request.getParameter("examDateId");
		TeacherExamSetting setting = new TeacherExamSetting();
		setting.setCode(code);
		setting.setName(name);
		setting.setContent(content);
		setting.setExamDateId(Integer.parseInt(examDateId));
		services.update(setting);
		services.updateAllSystemData(setting);
		List<TeacherExamSetting2> list = services.findExamSystem2ByCode(code);
		model.addAttribute("list",list);
		model.addAttribute("code",code);
		model.addAttribute("setting",setting);
		int hasResult = services.checkHasScore(code);
		model.addAttribute("hasResult", hasResult);
		return "/teacherExamSetting/teacherExamSettingEditTwo";
	}

	@RequestMapping(value = "reloadSecondStep/{code}")
	public String reloadSecondStep(Model model, @PathVariable("code") Integer code,HttpServletRequest request) {
		List<TeacherExamSetting2> list = services.findExamSystem2ByCode(code);
		TeacherExamSetting setting = new TeacherExamSetting();
		if (list != null && list.size() > 0){
			setting.setCode(list.get(0).getCode());
			setting.setName(list.get(0).getName());
			setting.setContent(list.get(0).getContent());
			setting.setExamDateId(list.get(0).getExamDateId());
			model.addAttribute("list",list);
		}
		model.addAttribute("code",code);
		model.addAttribute("setting",setting);
		int hasResult = services.checkHasScore(code);
		model.addAttribute("hasResult", hasResult);
		return "/teacherExamSetting/teacherExamSettingEditTwo";
	}

	@RequestMapping(value = "reloadThirdStep/{code}")
	public String reloadThirdStep(Model model, @PathVariable("code") Integer code,HttpServletRequest request) {
		List<TeacherExamSetting3> list = services.findExamSystem3ByCode(code);
		TeacherExamSetting setting = new TeacherExamSetting();
		if (list != null && list.size() > 0){
			setting.setCode(list.get(0).getCode());
			setting.setName(list.get(0).getName());
			setting.setContent(list.get(0).getContent());
			setting.setExamDateId(list.get(0).getExamDateId());
			model.addAttribute("list",list);
		}
		model.addAttribute("code",code);
		model.addAttribute("setting",setting);
		int hasResult = services.checkHasScore(code);
		model.addAttribute("hasResult", hasResult);
		return "/teacherExamSetting/teacherExamSettingEditThird";
	}

	@RequestMapping(value = "finishSystem/{code}", method = RequestMethod.POST)
	public String finishSystem(Model model, @PathVariable("code") Integer code,HttpServletRequest request,RedirectAttributes redirectAttributes) {
		String[] weightIds = request.getParameterValues("weightIds");
		String[] weight = request.getParameterValues("weight");
		String[] itemScoreIds = request.getParameterValues("itemScoreIds");
		String[] score = request.getParameterValues("score");
		services.updateThirdStep(weightIds, weight, itemScoreIds, score);
		redirectAttributes.addFlashAttribute("MESSAGE", "保存成功！");
		return "redirect:/teacherExamSetting";
	}

	@RequestMapping(value = "selectKind/{code}/{teacherId}")
	public String selectKind(@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,SearchParams searchParams,Page page, Model model, HttpServletRequest request) {
		List<Kind> list = services.findKindForSelect(code,teacherId,searchParams,page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		model.addAttribute("code", code);
		model.addAttribute("teacherId", teacherId);
		model.addAttribute("searchParams", searchParams);
		return "/teacherExamSetting/selectKindList";
	}

	@RequestMapping(value = "selectUser/{code}/{teacherId}/{kindId}/{userType}")
	public String selectUser(@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,@PathVariable("userType") Integer userType,SearchParams searchParams,Page page, Model model, HttpServletRequest request) {
		model.addAttribute("code", code);
		model.addAttribute("teacherId", teacherId);
		model.addAttribute("kindId", kindId);
		model.addAttribute("userType", userType);
		model.addAttribute("searchParams", searchParams);
		String returnUrl = "";
		if (userType == 1){
			List<Teacher> list = services.findTeachersForSystem(code,teacherId,kindId,searchParams,page);
			model.addAttribute("list", list);
			model.addAttribute("professionalList",professionalServices.find());
			model.addAttribute("teacherTypeList",teacherTypeServices.find());
			model.addAttribute("page", page);
			returnUrl = "/teacherExamSetting/selectTeacherSystemList";
		}else if (userType == 2){
			List<Student> list = services.findStudentsForSystem(code,teacherId,kindId,searchParams,page);
			model.addAttribute("list", list);
			model.addAttribute("professionalList",professionalServices.find());
			model.addAttribute("gradeList",gradeServices.find());
			model.addAttribute("classList",classServices.find());
			model.addAttribute("page", page);
			returnUrl = "/teacherExamSetting/selectStudentSystemList";
		}
		return returnUrl;
	}

	@RequestMapping(value = "toAddTeacher/{code}/{teacherId}/{kindId}")
	public String toAddTeacher(@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,SearchParams searchParams,Page page, Model model, HttpServletRequest request) {
		model.addAttribute("code", code);
		model.addAttribute("teacherId", teacherId);
		model.addAttribute("kindId", kindId);
		model.addAttribute("searchParams", searchParams);
		List<Teacher> list = services.selectTeachersForSystem(code, teacherId, kindId, searchParams, page);
		model.addAttribute("list", list);
		model.addAttribute("professionalList",professionalServices.find());
		model.addAttribute("teacherTypeList",teacherTypeServices.find());
		model.addAttribute("page", page);
		return "/teacherExamSetting/addTeacherSystemList";
	}

	@RequestMapping(value = "toAddStudent/{code}/{teacherId}/{kindId}")
	public String toAddStudent(@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,SearchParams searchParams,Page page, Model model, HttpServletRequest request) {
		model.addAttribute("code", code);
		model.addAttribute("teacherId", teacherId);
		model.addAttribute("kindId", kindId);
		model.addAttribute("searchParams", searchParams);
		List<Student> list = services.selectStudentsForSystem(code, teacherId, kindId, searchParams, page);
		model.addAttribute("list", list);
		model.addAttribute("professionalList",professionalServices.find());
		model.addAttribute("gradeList",gradeServices.find());
		model.addAttribute("classList",classServices.find());
		model.addAttribute("page", page);
		return "/teacherExamSetting/addStudentSystemList";
	}

	@RequestMapping(value = "addTeacherForThird/{code}/{teacherId}/{kindId}/{ids}")
	public String addTeacherForThird(Model model, @PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,@PathVariable("ids") String ids, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		services.saveUserForResult(code,teacherId,kindId,ids,1);
		redirectAttributes.addFlashAttribute("MESSAGE", "选择成功！");
		return "redirect:/teacherExamSetting/selectUser/"+code+"/"+teacherId+"/"+kindId+"/1";
	}

	@RequestMapping(value = "addStudentForThird/{code}/{teacherId}/{kindId}/{ids}")
	public String addStudentForThird(Model model, @PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,@PathVariable("ids") String ids, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		services.saveUserForResult(code,teacherId,kindId,ids,2);
		redirectAttributes.addFlashAttribute("MESSAGE", "选择成功！");
		return "redirect:/teacherExamSetting/selectUser/"+code+"/"+teacherId+"/"+kindId+"/2";
	}

	@RequestMapping(value = "saveKind/{code}/{teacherId}/{ids}")
	public String saveKind(Model model, @PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("ids") String ids, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		String[] idArrs ;
		if (ids != null && !"".equals(ids)){
			idArrs = ids.split(",");
			for (String kindId : idArrs) {
				services.saveKind(code,teacherId,kindId);
				services.copyStepThirdDatas(code,teacherId,kindId);
			}
		}
		redirectAttributes.addFlashAttribute("MESSAGE", "选择成功！");
		return "redirect:/teacherExamSetting/reloadSecondStep/"+code;
	}

	@RequestMapping(value = "deleteTeacher/{id}/{code}")
	public String deleteTeacher(@PathVariable("id") Integer id,@PathVariable("code") Integer code,RedirectAttributes redirectAttributes) {
		int dataCount = services.findCountByCode(code);
		if (dataCount > 1) {
			services.deleteTeacher(id, code);
			redirectAttributes.addFlashAttribute("MESSAGE", "删除成功！");
		}else{
			redirectAttributes.addFlashAttribute("ERROR", "请至少保留一个评价老师！");
		}
		return "redirect:/teacherExamSetting/update/"+code;
	}

	@RequestMapping(value = "deleteKind/{id}/{code}/{teacherId}")
	public String deleteKind(@PathVariable("id") Integer id,@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,RedirectAttributes redirectAttributes) {
		services.deleteKind(id, code, teacherId);
		redirectAttributes.addFlashAttribute("MESSAGE", "删除成功！");
		return "redirect:/teacherExamSetting/reloadSecondStep/"+code;
	}

	@RequestMapping(value = "deleteUser/{id}/{code}/{teacherId}/{kindId}/{userType}")
	public String deleteUser(@PathVariable("id") Integer id,@PathVariable("code") Integer code,@PathVariable("teacherId") Integer teacherId,@PathVariable("kindId") Integer kindId,@PathVariable("userType") Integer userType,RedirectAttributes redirectAttributes) {
		services.deleteUser(id,code,teacherId,kindId,userType);
		redirectAttributes.addFlashAttribute("MESSAGE", "删除成功！");
		return "redirect:/teacherExamSetting/selectUser/"+code+"/"+teacherId+"/"+kindId+"/"+userType;
	}
	
	@RequestMapping(value = "delete/{code}")
	public String delete(@PathVariable("code") Integer code,RedirectAttributes redirectAttributes) {
		services.delete(code);
		redirectAttributes.addFlashAttribute("MESSAGE", "删除成功！");
		return "redirect:/teacherExamSetting";
	}
	
}
